package pro.lingwu.station.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pro.lingwu.station.dto.db.Bus;
import pro.lingwu.station.service.BusService;

import javax.validation.Valid;


/**
 * @author @lingwu
 * @date created in 2021/11/16
 */
@Api(tags = "Bus")
@RestController
public class BusController {

    private final BusService busService;

    public BusController(BusService busService) {
        this.busService = busService;
    }

    @ApiOperation(value = "增加(购买)一辆车")
    @PostMapping("/bus")
    ResponseEntity<?> addBus(@RequestBody @Valid Bus bus){
        busService.addBus(bus);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "获取所有车(某个范围)的信息")
    @GetMapping("/bus")
    ResponseEntity<?> getBuses(int start, int size){
        return ResponseEntity.ok(busService.getAllBus(start, size));
    }

    @ApiOperation(value = "删除一辆车")
    @DeleteMapping("/bus/{bus-num}")
    ResponseEntity<?> rmBus(@PathVariable("bus-num") String busNum){
        busService.rmBus(busNum);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "更新辆车的信息")
    @PutMapping("/bus/{bus-num}")
    ResponseEntity<?> updateBus(@PathVariable("bus-num") String busNum,
                                @RequestBody Bus bus){
        busService.updateBus(bus, busNum);
        return ResponseEntity.ok("ok");
    }

}
